Public Partial Class FrmListadoRemitoCompra
    Inherits System.Web.UI.Page
    Public Remitos As CapaDeNegocios.nRemito = New CapaDeNegocios.nRemito
    Public Proveedor As CapaDeNegocios.nProveedor = New CapaDeNegocios.nProveedor
    Public Condicion As CapaDeNegocios.nCondicion = New CapaDeNegocios.nCondicion
    Public Detalle As CapaDeNegocios.nDetalleRemitoCompra = New CapaDeNegocios.nDetalleRemitoCompra
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            LblError.Visible = False
            LLenarGridRemitos()
            LLenarComboCondicion()
            LLenarcomboProv()
        End If
    End Sub
    Sub LLenarGridRemitos()
        Dim Estado As String
        Dim Condicion As String
        Dim Prov As Integer
        If CboEstado.SelectedItem.Text = "Ninguno" Then
            Estado = ""
        Else
            Estado = CboEstado.SelectedValue.ToString()
        End If
        If CboCondicion.SelectedValue.ToString() = -1 Then
            Condicion = ""
        Else
            Condicion = CboCondicion.SelectedValue.ToString()
        End If
        If CboProveedor.SelectedValue.ToString() = -1 Then
            Prov = 0
        Else
            Prov = CboProveedor.SelectedValue.ToString()
        End If
        GridRemitos.DataSource = Remitos.ListarTodos(0, "", Trim(TxtNumero.Text), Int(Prov), 0, Estado, "", Condicion)
        GridRemitos.DataBind()
    End Sub
    Sub LimpiarFormulario()
        TxtNumero.Text = ""
        CboCondicion.SelectedIndex = -1
        CboProveedor.SelectedIndex = -1
        CboEstado.SelectedIndex = -1
        GridRemitos.SelectedIndex = -1
        LblError.Visible = False
        LblError.Text = ""
        LLenarGridRemitos()
    End Sub
    Sub LLenarComboCondicion()
        CboCondicion.DataSource = Condicion.Listar(0, "", 0, "Compra")
        CboCondicion.DataTextField = "Descripcion"
        CboCondicion.DataValueField = "Codigocondicion"
        CboCondicion.DataBind()
    End Sub
    Sub LLenarcomboProv()
        CboProveedor.DataSource = Proveedor.Listar(0, "", "", "", "", "", 1, "")
        CboProveedor.DataTextField = "RazonSocial"
        CboProveedor.DataValueField = "Codigoprov"
        CboProveedor.DataBind()
    End Sub
    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        LLenarGridRemitos()
    End Sub

    Private Sub GridRemitos_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridRemitos.PageIndexChanging
        GridRemitos.PageIndex = e.NewPageIndex
        LLenarGridRemitos()
    End Sub
    'Protected Sub GridRemitos_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridRemitos.RowDataBound
    '    If e.Row.RowType = DataControlRowType.DataRow Then
    '        Dim a As LinkButton = e.Row.FindControl("LinkSeleccionar")
    '        Dim b As HiddenField = e.Row.FindControl("HFRemito")


    '        a.OnClientClick = "javascript:window.opener.document.getElementById('" + IdRemito + "').value = '" + b.Value.ToString() + "';window.opener.document.getElementById('" + IdBoton + "').click();window.close();"


    '    End If
    'End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        LimpiarFormulario()
    End Sub

    Protected Sub GridRemitos_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridRemitos.RowCommand
        If e.CommandName = "Eliminar" Then
            If e.CommandSource.parent.FindControl("LblEstado").text <> "Cancelado" Then
                Detalle.EliminarDetalle((e.CommandSource.parent.parent.FindControl("HFRemito").value))
                Remitos.EliminarRemito(Int(e.CommandSource.parent.parent.FindControl("HFRemito").value))
                Me.Response.Write("<script languaje='javascript'>window.alert('Eliminacion Correcta')</script>")
                LLenarGridRemitos()
            Else
                LblError.Visible = True
                LblError.Text = "Error, no puede eliminar un remito Facturado."
            End If
        End If
    End Sub

    Protected Sub GridRemitos_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridRemitos.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim a As LinkButton = e.Row.FindControl("LnkMasDatos")
            Dim b As HiddenField = e.Row.FindControl("HFRemito")
            a.OnClientClick = "javascript:var popup = window.open('FrmDetalleRemitoCompra.aspx?IDRemito=" + b.Value.ToString() + "','popup','Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
        End If
    End Sub
End Class